{{define "title"}}{{ .BuilderPage.Builder.Id.Builder }} | Builder{{end}}

{{define "head"}}
  <link rel="stylesheet" href="/static/common/css/builder.css" type="text/css">
{{end}}

{{define "body"}}
{{ $request := .Request }}

{{with .BuilderPage}}

{{ $hasChanges := .HasChanges }}

<h1>Builder <a href="{{ pagedURL $request -1 "EMPTY" }}">{{.Builder.Id.Builder}}</a></h1>
{{ if .Builder.Config.DescriptionHtml }}
  <p id="builder-description">{{ .Builder.Config.DescriptionHtml | sanitizeHTML }}</p>
{{ end }}

<div class="prelude">
  <div class="column">
  {{ if .MachinePool }}
    {{ with .MachinePool }}
      <h2>
      {{ if .SwarmingHost }}
        <a href="{{ .SwarmingURL }}">Machine Pool</a>
      {{ else }}
        Machine Pool
      {{ end }}
      </h2>
      <table>
        <tr>
          <td>Idle</td><td>{{ .Idle }}</td>
          <td class="graph">
            {{ if gt .Idle 0 }}
              <div class="bar idle" style="width: {{ percent .Idle .Total }}%"></div>
            {{ end }}
          </td>
        </tr>
        <tr>
          <td>Busy</td><td>{{ .Busy }}</td>
          <td class="graph">
            {{ if gt .Busy 0 }}
              <div class="bar busy" style="width: {{ percent .Busy .Total }}%"></div>
            {{ end }}
          </td>
        </tr>
        <tr>
          <td>Quarantined</td><td>{{ .Quarantined }}</td>
          <td class="graph">
            {{ if gt .Quarantined 0 }}
              <div class="bar quarantined"
                   style="width: {{ percent .Quarantined .Total }}%"></div>
            {{ end }}
          </td>
        </tr>
        <tr>
          <td>Offline</td><td>{{ .Offline }}</td>
          <td class="graph">
            {{ if gt .Offline 0 }}
              <div class="bar offline"
                   style="width: {{ percent .Offline .Total }}%"></div>
            {{ end }}
          </td>
        </tr>
      </table>
      <details>
        <summary>{{ .Total }} bots</summary>
        {{ if eq (len .Bots) 1000 }}
          Note: This list may be incomplete, click on the Machine Pool link above to see all bots.
        {{ end }}
        <table class="info">
          <tr><th>Name</th><th>Status</th><th>Last Seen</th></tr>
          {{ range .Bots }}
            <tr>
              <td>{{ .Label.HTML }}</td>
              <td class="status-{{ .Status.String }}">{{ .Status.String }}</td>
              <td>{{ .LastSeen | localTime "N/A" }}</td>
            </tr>
          {{ end }}
        </table>
      </details>
    {{ end }}
  {{ end }}
  </div>

  {{/*
    TODO(iannucci): add a checkbox to optionally display experimental builds
  */}}
  <div class="column">
    <h2>Started builds (
      {{- len .StartedBuilds -}}
      {{- if not .StartedBuildsComplete }} most recent{{end -}})
    </h2>

    <ul>
    {{ range .StartedBuilds }}
      <li>{{ .Link.HTML }}
        [Running for: {{ duration .StartTime .Now }}]
      </li>
    {{ end }}
    </ul>
  </div>

  <div class="column">
    <h2>Scheduled builds (
      {{- len .ScheduledBuilds -}}
      {{- if not .ScheduledBuildsComplete }} most recent{{end -}})
    </h2>
    <ul>
    {{- range .ScheduledBuilds -}}
      <li>
        {{- .Link.HTML }}
        <small>({{ .CreateTime | localTimestamp "" }}, waiting {{ duration .CreateTime .EndTime .Now }})</small>
      </li>
    {{- end -}}
    </ul>
  </div>

  {{- if .Views -}}
  <div class="column">
    <h2>Views:</h2>
    <ul>
    {{- range .Views -}}
      <li>{{ .HTML }}</li>
    {{- end -}}
    </ul>
  </div>
  {{- end }}
</div>

<div class="clear"></div>

<div class="main">
  <h2>Ended builds</h2>
  {{ if not .EndedBuilds }}
    No builds
  {{ else }}
    <table class="info">
      <tr>
        <th>Create time</th>
        <th>End time</th>
        <th>Run duration</th>
        <th>Commit</th>
        {{- if $hasChanges }}
        <th>Changes</th>
        {{ end -}}
        <th>Status</th>
        <th>Build #</th>
        <th>Summary</th>
      </tr>

      {{ range .EndedBuilds }}
        <tr>
          <td>{{ .CreateTime | localTimestamp "" }}</td>
          <td>{{ .EndTime | localTimestamp "" }}</td>
          <td>{{ duration .StartTime .EndTime .Now }}</td>
          <td class="revision">{{.CommitLinkHTML}}</td>
          {{- if $hasChanges }}
          <td>
            {{ range $i, $link := .ChangeLinks }}
              {{- if gt $i 0 }}, {{ end -}}
              {{- $link.HTML -}}
            {{end}}
          </td>
          {{- end }}
          <td class="status {{.Status}}">{{.Status}}</td>
          <td>{{ .Link.HTML }}</td>
          <td class="left summary-markdown">{{ .SummaryMarkdown | renderMarkdown }}</td>
        </tr>
      {{ end }}
    </table>
  {{ end }}
</div>

<div class="epiloge">
  {{ if .PrevPageToken }}<a href="{{ pagedURL $request 0 .PrevPageToken }}">Prev</a>{{ end }}
  (Show:
  <a href="{{ pagedURL $request -1 "" }}">default</a>
  <a href="{{ pagedURL $request 25 "" }}">25</a>
  <a href="{{ pagedURL $request 50 "" }}">50</a>
  <a href="{{ pagedURL $request 100 "" }}">100</a>
  <a href="{{ pagedURL $request 200 "" }}">200</a>
  )
  {{ if .NextPageToken }}
    <a href="{{ pagedURL $request 0 .NextPageToken}}">Next</a>
  {{ end }}
</div>

{{end}}
{{end}}
